/**
    @author: dt
    @date: 2025-06-23
    @description: 复合节点基类
*/
using System.Collections.Generic;
using UnityEngine;

namespace DLib.BehaviorTree
{
    /// <summary>
    /// 复合节点基类
    /// 用于实现可以包含多个子节点的节点类型
    /// 如选择器(Selector)和序列(Sequence)
    /// </summary>
    public abstract class CompositeNode : Node
    {
        /// <summary>当前执行的子节点索引</summary>
        protected int currentChildIndex = 0;

        /// <summary>
        /// 默认构造函数
        /// </summary>
        public CompositeNode() : base() { }
        
        /// <summary>
        /// 带子节点的构造函数
        /// </summary>
        /// <param name="children">子节点列表</param>
        public CompositeNode(List<Node> children) : base(children) { }

        /// <summary>
        /// 评估复合节点状态
        /// 如果没有子节点，直接返回成功
        /// 否则调用子类实现的EvaluateChildren方法
        /// </summary>
        /// <returns>节点执行状态</returns>
        public override NodeState Evaluate()
        {
            if (children.Count == 0)
                return NodeState.Success;

            return EvaluateChildren();
        }

        /// <summary>
        /// 评估子节点的抽象方法
        /// 子类必须实现此方法来定义具体的子节点评估逻辑
        /// </summary>
        /// <returns>节点执行状态</returns>
        protected abstract NodeState EvaluateChildren();

        /// <summary>
        /// 重置复合节点状态
        /// 将当前子节点索引重置为0，并递归重置所有子复合节点
        /// </summary>
        public override void Reset()
        {
            base.Reset();
            currentChildIndex = 0;
            foreach (Node child in children)
            {
                child.Reset();
            }
        }
    }
} 